/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.muni.fi.pb138.xml.wrda.servise;

import cz.muni.fi.pb138.xml.wrda.core.Wsdl;
import cz.muni.fi.pb138.xml.wrda.dao.WsdlDao;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author N
 */
public class WSDLServiceImpl implements WSDLService{
    
    @Autowired
    WsdlDao dao;

    @Override
    public void save(Wsdl wsdl) {
        this.dao.save(wsdl);
    }

    @Override
    public void delete(Wsdl wsdl) {
        this.dao.delete(wsdl);
    }

    @Override
    public void printAll() {
        this.dao.printAll();
    }
    
    @Override
    public List<Wsdl> getAllEntries() {
        return this.dao.getAllEntries();
    }
    
    @Override
    public boolean saveFile(InputStream file, String originalFileName) {
        Wsdl entry = new Wsdl();

        try {
            entry.setDoc(new SAXBuilder().build(file));
        } catch (JDOMException ex) {
            Logger.getLogger(WSDLServiceImpl.class.getName()).log(Level.SEVERE, "Error in file format.", ex);
            return false;
        } catch (IOException ex) {
            Logger.getLogger(WSDLServiceImpl.class.getName()).log(Level.SEVERE, "IO error in creating Document.", ex);
            return false;
        }

        entry.setFileName(originalFileName);
        entry.parse();
        
        this.dao.save(entry);
        return true;
    }
    
    @Override
    public Object getAllEntriesDistinctNames() {
        return this.dao.getAllEntriesDistinctNames();
    }
    
    @Override
    public void deleteEntryByTimeStamp(String timeStamp) {
        this.dao.deleteEntryByTimeStamp(timeStamp);
    }
    
    @Override
    public void deleteAllEntryByName(String fileName) {
        this.dao.deleteAllEntryByName(fileName);
    }
    
    @Override
    public Object getEntryByFileNameAndTimeStamp(String name, String timeStamp) {
        return this.dao.getEntryByFileNameAndTimeStamp(name, timeStamp);
    }
    
    @Override
    public Object getAllEntriesByFileName(String fileName) {
        return this.dao.getAllEntriesByFileName(fileName);
    }
    
    @Override
    public StringReader getWsdlContentAsInputStream(String timeStamp) {
        Wsdl entry = this.dao.getEntryByTimestamp(timeStamp);
        XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
        StringReader reader  = new StringReader(out.outputString(entry.getDoc()));
        return reader;
    }
    
}
